#include <iostream>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;
int n, m;
const int N = 55;
char ans[N][N];
vector<pair<int, int>> points[2];
int dx[4] = { 0,-1,0,1 },dy[4] = { -1,0,1,0 };


void dfs(int a, int b, vector<pair<int,int>>& q)
{
	q.push_back({ a,b });
	ans[a][b] = '.';
	for (int i = 0; i < 4; i++)
	{
		int x = a + dx[i], y = b + dy[i];
		if (x >= 0 && x < n && y >= 0 && y < m && ans[x][y] == 'X')
		{
			dfs(x, y, q);
		}

	}

}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for(int j=0;j<m;j++)
		    cin>>ans[i][j];
	int k = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (ans[i][j] == 'X')
				dfs(i, j, points[k++]);

	int col = 100;
	for (auto& a : points[0])
	{
	    for (auto& b : points[1])
		{
			col = min(col, abs(a.first - b.first) + abs(a.second - b.second));
		}
	}	
	cout << col-1;
	return 0;
}
